/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.mapper;

import org.retabel.persistence.entity.document.ExportConfiguration;
import org.retabel.persistence.entity.document.SectionConfiguration;
import org.retabel.persistence.entity.report.ReportDesign;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * Class description.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 15:44 12.08.11
 *
 * @author Peter Herber
 * @version 1.0
 */
@Stateless
public class XmlSectionConfigurationMapperBean extends XmlSectionConfigurationMapperBaseBean implements XmlSectionConfigurationMapper {

    /** {@inheritDoc} */
    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    @Override
    public SectionConfiguration unmarshal(byte[] sectionConfigurationData) throws XmlConfigurationMappingException {
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(sectionConfigurationData);

            xsd.sectionconfiguration.SectionConfiguration sectionConfiguration = unmarshal(inputStream);

            return mapSectionConfiguration(sectionConfiguration);
        } catch (JAXBException e) {
            throw new XmlConfigurationMappingException("Error while parsing XmlConfiguration: " + e.getMessage(), e);
        }
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public JAXBElement<xsd.sectionconfiguration.SectionConfiguration> marshal(SectionConfiguration sectionConfiguration) {
        xsd.sectionconfiguration.ObjectFactory sectionConfigurationObjectFactory =
            new xsd.sectionconfiguration.ObjectFactory();

        xsd.sectionconfiguration.SectionConfiguration xmlSectionConfiguration =
            sectionConfigurationObjectFactory.createSectionConfiguration();
        xmlSectionConfiguration.setName(sectionConfiguration.getName());
        xmlSectionConfiguration.setVersion(sectionConfiguration.getVersion());

        ReportDesign reportDesign = sectionConfiguration.getReportDesign();
        xmlSectionConfiguration.setDesignName(reportDesign.getName());
        xmlSectionConfiguration.setDesignVersion(reportDesign.getReportDesignPackage().getVersion());
        xmlSectionConfiguration.setDesignPackage(reportDesign.getReportDesignPackage().getName());

        xsd.exportconfiguration.ObjectFactory exportConfigurationObjectFactory =
            new xsd.exportconfiguration.ObjectFactory();

        xsd.exportconfiguration.ExportConfiguration xmlExportConfiguration =
            exportConfigurationObjectFactory.createExportConfiguration();

        for (ExportConfiguration exportConfiguration : sectionConfiguration.getExportConfigurations()) {
            xsd.exportconfiguration.ExportConfiguration.Export xmlExport =
                exportConfigurationObjectFactory.createExportConfigurationExport();
            xmlExport.setRef(exportConfiguration.getName());

            xmlExportConfiguration.getExport().add(xmlExport);
        }

        xmlSectionConfiguration.setExportConfigurations(xmlExportConfiguration);

        return sectionConfigurationObjectFactory.createSectionConfiguration(xmlSectionConfiguration);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public SectionConfiguration mapSectionConfiguration(xsd.sectionconfiguration.SectionConfiguration sectConfiguration)
        throws XmlConfigurationMappingException {
        SectionConfiguration sectionConfiguration = super.mapSectionConfiguration(sectConfiguration);

        //additionally set name and version
        sectionConfiguration.setName(sectConfiguration.getName());
        sectionConfiguration.setVersion(sectConfiguration.getVersion());

        return sectionConfiguration;
    }

    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    public xsd.sectionconfiguration.SectionConfiguration unmarshal(InputStream inputStream) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(xsd.sectionconfiguration.ObjectFactory.class);
        Unmarshaller u = jc.createUnmarshaller();
        return ((JAXBElement<xsd.sectionconfiguration.SectionConfiguration>) u.unmarshal(inputStream)).getValue();
    }
}
